package main

import (
	"fmt"
	"strconv"

	"github.com/gomodule/redigo/redis"
)

var redisConn redis.Conn
func init(){
	//redisConn=new(redis.Conn)
}
func main() {
	var err error
	redisConn, err = redis.Dial("tcp", "127.0.0.1:6379")
	if err != nil {
		fmt.Println("Connect to redis error", err)
		return
	}
	defer redisConn.Close()

	_, err = redisConn.Do("SET", "mykey", "superWang")
	if err != nil {
		fmt.Println("redis set failed:", err)
	}

	username, err := redis.String(redisConn.Do("GET", "mykey"))
	if err != nil {
		fmt.Println("redis get failed:", err)
	} else {
		fmt.Printf("Get mykey: %v \n", username)
	}
	initArticle(1)
	viewCount, err := redis.String(redisConn.Do("GET", getBlogPageViewKeys(1)))
	fmt.Println(viewCount)
	for i:=0;i<1000;i++{
		view(1)
		viewCount, err = redis.String(redisConn.Do("GET", getBlogPageViewKeys(1)))
		fmt.Println(viewCount)
	}

}

func Set(key string,value string){
	redisConn.Do("SET",key,value)
}

func Get(Key string)(interface{},error){
	return redisConn.Do("GET",Key)
}

func initArticle(blogId int){
	//c.conn.Do("SET",getBlogPageViewKeys(blogId),"0")
	redisConn.Do("SET",getBlogPageViewKeys(blogId),"0")
}

func view(blogId int){
	redisConn.Do("INCR",getBlogPageViewKeys(blogId))
}

func getBlogPageViewKeys(articleId int)string{
	return "blog:"+strconv.Itoa(articleId)+":viewCount"

}

func init(){

}